<?php
/**
 * 
 * @authors Liujinbi (857053791@QQ.com)
 * @date    2017-08-14 18:46:03
 * @version $Id$
 */
namespace app\finance\controller;
use think\Db;
use think\Request;
/**
 * 数据统计
 */
class Statistics extends Base {
    
    function index(){
    	$iw=Request()->param();

    	$start_time=isset($iw['start_time'])?strtotime($iw['start_time']):'';
    	$end_time=isset($iw['end_time'])?strtotime($iw['end_time']):'';

    	if($end_time){
            $_end_time=getOneDay($end_time);
            // $_end_time=$_end_time['et'];
            if ($end_time==$_end_time['st']) {
                $end_time=$_end_time['et'];
            }
        }else{
            $end_time=getOneDay();
            $end_time=$end_time['et'];
        }

        if ($start_time) {
            // $start_time=getOneDay($start_time);
            // $start_time=$start_time['st'];
        }else{
            $start_time=getDayRegion($end_time,3);
            $start_time=$start_time['st'];
        }
        
        $iw['start_time']=date('Y-m-d H:i:s',$start_time);
        $iw['end_time']=date('Y-m-d H:i:s',$end_time);

        if ($iw['selt']!=='0') {
            $iw['selt']=$iw['selt']?:10;
        }
    	$data['w']=$iw;

        $dayMap=getDayMapSE($start_time,$end_time);
        //订单
    	$_order = Db::field('order_id,pay_money,pay_time,order_status')->name('order')->where("pay_status=1 and pay_time between '{$iw['start_time']}' and '{$iw['end_time']}'")->select();
        $order_finish = []; $order_nfinish = [];
        foreach ($_order as $k => $v) {
            if ($v['order_status']==100 || $v['order_status']==10) {
                $order_finish[date('Y-m-d',strtotime($v['pay_time']))]['money'] += $v['pay_money'];
            }else{
                $order_nfinish[date('Y-m-d',strtotime($v['pay_time']))]['money'] += $v['pay_money'];
            }
        }
    	//补价
        $_change = Db::field('order_id,price,pay_time')->name('order_change')->where("pay_status=1 and pay_time between '{$iw['start_time']}' and '{$iw['end_time']}'")->select();
        $changeent = [];$changeout = [];
        foreach ($_change as $k => $v) {
            if ($v['price']<0) {
                $changeout[date('Y-m-d',strtotime($v['pay_time']))]['money'] += -1*$v['price'];
            }else{
                $changeent[date('Y-m-d',strtotime($v['pay_time']))]['money'] += $v['price'];
            }
            
        }
        
        //退货
        $_refundgoods = Db::field('order_id,(return_money*rg_num) as money, pay_time')->name('order_goods_return')->where("pay_status=1 and pay_time between '{$iw['start_time']}' and '{$iw['end_time']}'")->select();
        $refundgoods=[];
        foreach ($_refundgoods as $k => $v) {
            $refundgoods[date('Y-m-d',strtotime($v['pay_time']))]['money'] += $v['money'];
        }
        //采购支款
        $_appropriation = Db::field('create_time,type,money')->name('appropriation')->where("create_time between '{$iw['start_time']}' and '{$iw['end_time']}'")->select();
        $appropriation=[];
        foreach ($_appropriation as $k => $v) {
            if ($v['type']) { //收到退款
                $appropriation[date('Y-m-d',strtotime($v['create_time']))]['money'] -= $v['money'];
            }else{
                $appropriation[date('Y-m-d',strtotime($v['create_time']))]['money'] += $v['money'];
            }
            
        }

        $arr = []; $ent_finish_total=0;$ent_nfinish_total=0;$out_pur_total=0;$out_refund_total=0;
        foreach ($dayMap as $k => $v) {
            $_date = date('Y-m-d',$k);
            $arr[$_date]['ent_finish'] +=  $order_finish[$_date]['money'];
            $arr[$_date]['ent_finish'] +=  $changeent[$_date]['money'];

            $arr[$_date]['ent_nfinish'] +=  $order_nfinish[$_date]['money'];

            $arr[$_date]['out_pur'] += $appropriation[$_date]['money'];

            $arr[$_date]['out_refund'] += $refundgoods[$_date]['money'];
            $arr[$_date]['out_refund'] += $changeout[$_date]['money'];

            $ent_finish_total += $arr[$_date]['ent_finish'];
            $ent_nfinish_total += $arr[$_date]['ent_nfinish'];
            $out_pur_total += $arr[$_date]['out_pur'];
            $out_refund_total += $arr[$_date]['out_refund'];
        }
        $data['list'] =$arr;
        $data['ent_finish_total'] = $ent_finish_total;
        $data['ent_nfinish_total'] = $ent_nfinish_total;
        $data['out_pur_total'] = $out_pur_total;
        $data['out_refund_total'] = $out_refund_total;

        foreach ($dayMap as $k => $v) {
            $data['dayMap'] .= '"'.date('Y-m-d',$k).'",';
        }
        $data['dayMap'] = '['.rtrim($data['dayMap'],',').']'; // 按天显示
        $E=''; $O='';
        foreach ($arr as $k => $v) {
            $E .= $v['ent_finish']+$v['ent_nfinish'].',';
            $O .= $v['out_pur']+$v['out_refund'].',';
        }
        $data['E'] = '['.rtrim($E,',').']';
        $data['O'] = '['.rtrim($O,',').']';
        return $this->fetch('',$data);
    }

}